package com.atguigu.gulimall.product.service.impl;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.atguigu.gulimall.product.dao.AttrGroupDao;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrGroupService;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.gulimall.product.vo.AttrGroupWithAttrsVo;
import com.atguigu.gulimall.product.vo.SpuItemAttrGroupVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Resource
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long categoryId) {
        // params 参数中可能包含一个字符串 key 作为多字段模糊匹配的查询条件
        String key = (String) params.get("key");
        // select * from pms_attr_group where category_id = ? and (attr_group_id = ? or attr_group_name like ?)
        LambdaQueryWrapper<AttrGroupEntity> wrapper = new LambdaQueryWrapper<AttrGroupEntity>();
        if (StringUtils.hasText(key)) {
            wrapper.and(w -> w.eq(AttrGroupEntity::getAttrGroupId, key).or().like(AttrGroupEntity::getAttrGroupName, key));
        }

        // 项目约定：如果传入的参数 categoryId 为 0，则查询所有数据
        if (categoryId == 0) {
            // Query 工具类封装了分页，排序，查询条件，返回 MyBatis-Plus 提供的 IPage 分页对象
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper);
            // PageUtils 分页工具类封装了从分页对象提取到的分页相关数据，如总记录数、每页记录数、总页数、当前页数、列表数据，返回 PageUtils 对象
            return new PageUtils(page);
        } else {
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper.eq(AttrGroupEntity::getCatelogId, categoryId));
            return new PageUtils(page);
        }
    }

    /**
     * 根据分类 id 查询出该分类下的所有属性分组，以及每个属性分组下的所有属性
     * @param catelogId
     * @return
     */
    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        // 1. 根据 catelogId 查询出该分类下的所有属性分组
        List<AttrGroupEntity> attrGroupList = this.list(new LambdaQueryWrapper<AttrGroupEntity>().eq(AttrGroupEntity::getCatelogId, catelogId));

        // 2. 查询出每个属性分组下的所有属性
        List<AttrGroupWithAttrsVo> collect = attrGroupList.stream().map(attrGroup -> {
            AttrGroupWithAttrsVo attrGroupWithAttrsVo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(attrGroup, attrGroupWithAttrsVo);
            List<AttrEntity> relationAttrList = attrService.getRelationAttr(attrGroup.getAttrGroupId());
            attrGroupWithAttrsVo.setAttrs(relationAttrList);
            return attrGroupWithAttrsVo;
        }).collect(Collectors.toList());

        return collect;
    }

    /**
     * 查询出该 spu 下的所有属性分组，以及每个属性分组下的所有属性
     *
     * @param spuId
     * @param catalogId
     * @return
     */
    @Override
    public List<SpuItemAttrGroupVo> getAttrGroupWithAttrsBySpuId(Long spuId, Long catalogId) {
        return baseMapper.getAttrGroupWithAttrsBySpuId(spuId, catalogId);
    }
}